package cn.sunjinxin.trade.application.point.order;

import cn.sunjinxin.savior.ext.anno.ExtensionPoint;
import cn.sunjinxin.trade.domain.ability.OrderAbility;
import cn.sunjinxin.trade.domain.enums.OrderType;
import cn.sunjinxin.trade.domain.gateway.OrderGateway;
import cn.sunjinxin.trade.domain.model.Order;
import cn.sunjinxin.trade.domain.model.ProductSpu;
import cn.sunjinxin.trade.domain.model.entity.Distributor;
import cn.sunjinxin.trade.domain.model.entity.Traveler;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author issavior
 */
@ExtensionPoint(bizCode = "b2c", scenario = "order")
public class B2bOrderPoint extends OrderAbility {

    @Resource
    private OrderGateway orderGateway;

    @Override
    public Order create(ProductSpu productSpu, Distributor distributor, List<Traveler> travelers) {
        Order b2bOrder = Order.initOrder(OrderType.B2B, productSpu, distributor, travelers);
        orderGateway.save(b2bOrder);
        productSpu.getProductSkus().forEach(sku -> {
            Order b2pOrder = Order.initOrder(OrderType.B2C, productSpu.of(Collections.singletonList(sku)), distributor, travelers);
            orderGateway.save(b2pOrder);
        });
        return b2bOrder;
    }
}
